<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\shiyao;

use app\common\model\shiyao\Pi as PiModel;
use app\common\model\store\Setting as SettingModel;
use think\facade\Db;

/**
 * 申办方模型
 * Class StoreUser
 * @package app\store\model
 */
class Pi extends PiModel {

	/**
	 * 获取用户列表
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => []]);
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}

		// 关键词搜索
		!empty($params['search']) && $query->where('p.project_name|c.company_name|p.project_code', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')

			->join('shiyao_project_center center', 'center.id=p.center_id')
			->join('shiyao_project pr', 'pr.project_id=p.project_id')
			->join('shiyao_company c', 'c.company_id=pr.company_id')
			->where('p.is_delete', '=', '0')
			->where('p.user_id', '=', $params['userId'])
			->field('p.*,pr.*,c.*,center.center_name,center.center_code,center.create_time,center.pm_id')
			->order(['center.create_time' => 'desc'])
			->paginate(15)->each(function ($item, $key) {
			$item['pms'] = Db::name('shiyao_project_center_pm')->alias('pm')
				->join('user', 'user.user_id=pm.pm_id')
				->where('pm.center_id', $item['center_id'])
				->field('user.*')->select()->toArray();
			$item['pi'] = Db::name('shiyao_project_center_pi')->alias('pi')
				->join('user', 'user.user_id=pi.user_id')
				->where('pi.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,pi.*')->select()->toArray();
			$item['crc'] = Db::name('shiyao_project_center_crc')->alias('crc')
				->join('user', 'user.user_id=crc.user_id')
				->where('crc.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,crc.*')->select()->toArray();

			$item['pm'] = Db::name('user')->where('user_id', $item['pm_id'])->find();
			$item['testee_count'] = Db::name('shiyao_project_center_testee')
				->where('center_id', $item['center_id'])->count();
			$item['buzhu_count'] = Db::name('shiyao_project_pay_log')
				->where('center_id', $item['center_id'])->count();
			return $item;
		});

		return $list;
	}

	public function getListAll($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => []]);
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}

		// 关键词搜索
		!empty($params['search']) && $query->where('p.project_name|c.company_name|p.project_code', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')

			->join('shiyao_project_center center', 'center.id=p.center_id')
			->join('shiyao_project pr', 'pr.project_id=p.project_id')
			->join('shiyao_company c', 'c.company_id=pr.company_id')
			->where('p.is_delete', '=', '0')
			->where('p.user_id', '=', $params['userId'])
			->field('p.*,pr.*,c.*,center.center_name,center.center_code,center.create_time,center.pm_id')
			->order(['center.create_time' => 'desc'])
			->select()->toArray();
		foreach ($list as &$item) {
			$item['pms'] = Db::name('shiyao_project_center_pm')->alias('pm')
				->join('user', 'user.user_id=pm.pm_id')
				->where('pm.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile')->select()->toArray();
			$item['pi'] = Db::name('shiyao_project_center_pi')->alias('pi')
				->join('user', 'user.user_id=pi.user_id')
				->where('pi.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,pi.*')->select()->toArray();
			$item['crc'] = Db::name('shiyao_project_center_crc')->alias('crc')
				->join('user', 'user.user_id=crc.user_id')
				->where('crc.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,crc.*')->select()->toArray();

			$item['pm'] = Db::name('user')->where('user_id', $item['pm_id'])->field('mobile,nick_name')->find();
			$item['testee_count'] = Db::name('shiyao_project_center_testee')
				->where('center_id', $item['center_id'])->count();
			$item['buzhu_count'] = Db::name('shiyao_project_pay_log')
				->where('center_id', $item['center_id'])->count();
		}

		return $list;
	}
	/**
	 * 新增
	 * @param array $data
	 * @return mixed
	 * @throws BaseException
	 */
	// public function increased($id, $projectId, array $data) {

	// 	//新增Pi
	// 	$userInfo = Db::name('user')->where('mobile', $data['phone'])->find();
	// 	if ($userInfo) {

	// 		$userId = $userInfo['user_id'];
	// 		$userData['grade_id'] = 2;
	// 		$userData['nick_name'] = $data['name'];
	// 		$userData['is_delete'] = 0;
	// 		Db::name('user')->where('user_id', $userId)->update($userData);
	// 	} else {
	// 		$userData['nick_name'] = $data['name'];
	// 		$userData['mobile'] = $data['phone'];
	// 		$userData['grade_id'] = 2;
	// 		$userData['password'] = '123456';
	// 		$userData['create_time'] = time();
	// 		$userData['store_id'] = self::$storeId;
	// 		$userId = Db::name('user')->insertGetId($userData);
	// 	}

	// 	$data = array_merge($data, [
	// 		'center_id' => $id,
	// 		'user_id' => $userId,
	// 		'project_id' => $projectId,
	// 		'store_id' => self::$storeId,
	// 	]);
	// 	$this->save($data);

	// 	return true;

	// }

	public static function increased($id, $projectId, array $piList) {
		$password = SettingModel::getItem('trade', 10001)['order']['password'];

		Db::name('shiyao_project_center_pi')->where('project_id', $projectId)->where('center_id', $id)->delete();
		$dataset = [];
		foreach ($piList as $item) {
			unset($item['pay']);
			//unset($item['pi_id']);
			//新增Pi
			$userInfo = Db::name('user')->where('mobile', $item['phone'])->find();
			if ($userInfo) {
				Db::name('user')->where('user_id', $userInfo['user_id'])->update(['nick_name' => $item['name'], 'is_delete' => 0]);
				$userId = $userInfo['user_id'];
			} else {
				$userData['nick_name'] = $item['name'];
				$userData['mobile'] = $item['phone'];
				$userData['grade_id'] = 2;
				$userData['password'] = $password;
				$userData['create_time'] = time();
				$userData['store_id'] = self::$storeId;
				$userId = Db::name('user')->insertGetId($userData);
			}

			$data = array_merge($item, [
				'center_id' => $id,
				'user_id' => $userId,
				'project_id' => $projectId,
				'store_id' => self::$storeId,
			]);
			$dataset[] = $data;

		}
		//$this->saveAll($dataset);
		return (new static )->addAll($dataset);
		//return true;

	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {
		$data['project_name'] = $data['project_name'];

		$data['store_id'] = self::$storeId;

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增
			$this->save($data);

		});
		return true;
	}

	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	public function audit(array $data) {

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	/**
	 * 软删除
	 * @return false|int
	 */
	public function setDelete() {

		return $this->transaction(function () {
			// 删除对应的角色关系
			UserRole::deleteAll([['store_user_id', '=', (int) $this['store_user_id']]]);
			return $this->save(['is_delete' => 1]);
		});
	}
	public static function detail(int $projectId) {

		$detail = self::get(['project_id' => $projectId]);

		return $detail;
	}

}
